package cn.edu.xmu.oomall.customer.service.mq;

import cn.edu.xmu.oomall.core.util.JacksonUtil;
import cn.edu.xmu.oomall.customer.mapper.CouponPoMapper;
import cn.edu.xmu.oomall.customer.model.po.CouponPo;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 消息消费者
 * @author Ming Qiu
 * @date Created in 2020/11/7 22:47
 * @date ModifiedBy XJX 2021-12-14 12:10:25
 **/
@Service
@RocketMQMessageListener(topic = "coupon-topic", consumeMode = ConsumeMode.CONCURRENTLY, consumeThreadMax = 10, consumerGroup = "coupon-group")
public class CouponConsumerListenner implements RocketMQListener<String> {
    private static final Logger logger = LoggerFactory.getLogger(CouponConsumerListenner.class);

    @Autowired
    CouponPoMapper couponPoMapper;

    @Override
    public void onMessage(String message) {
        CouponPo couponPo = JacksonUtil.toObj(message,CouponPo.class);
        logger.info("onMessage: got message coupon =" + couponPo +" time = "+ LocalDateTime.now());
        couponPoMapper.insert(couponPo);
        /**
         * TODO:减少对应活动的优惠券库存量
         */
    }
}
